<?php
/**
 * cms 信息逻辑层
 * @author maniac
 */
namespace app\common\logic;

use app\common\model\Columns;
use app\common\model\Mods;
use app\common\model\Fields;
use think\Db;

class Contents extends Base
{
    /**
     * 添加/编辑逻辑处理
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function save($data)
    {
        $result = parent::save($data);
        if ($result !== true && $result) {
            // 添加
            $data['id'] = $result;
        }
        $info = Db::name($data['tbname'])->field('id')->where('id', $data['id'])->find();
        if ($info) {
            Db::name($data['tbname'])->strict(false)->update($data);
        } else {
            Db::name($data['tbname'])->strict(false)->insert($data);
        }
        return $result;
    }
    
    /**
     * 重组提交数据
     * @param  array $data 请求的信息
     */
    public function recombine($data)
    {
        $data['column_id']       = (int)$data['column_id'];
        $data                    = $this->validData($data);
        $data['access_role_ids'] = isset($data['access_role_ids']) && $data['access_role_ids'] ? implode(',', $data['access_role_ids']) : '';
        $data['checked']         = isset($data['checked']) ? (int)$data['checked'] : 0;
        $data['diy_browse_nums'] = (int)$data['diy_browse_nums'];
        $data['isgood']          = (int)$data['isgood'];
        $data['titlepic']        = isset($data['titlepic']) ? (int)$data['titlepic'] : 0;
        $data['ispic']           = $data['titlepic'] ? 1 : 0;
        if (isset($data['id']) && $data['id']) {
            // 编辑
        } else {
            // 添加
            $data['userid']   = session('userid', '', session_prefix());
            $data['username'] = session('username', '', session_prefix());
        }
        unset($data['actual_browse_nums']);
        return $data;
    }
    
    /**
     * 验证请求数据
     * @param  array $data 请求的信息
     */
    private function validData($data)
    {
        if (!$data['column_id']) exception('请选择要添加信息的栏目');
        $column = Columns::field('tbname,mod_id')->where('id', $data['column_id'])->find();
        if (!$column) exception('栏目不存在');
        $data['tbname'] = $column['tbname'];
        $data['mod_id'] = $column['mod_id'];
        $type           = isset($data['ismember']) && $data['ismember'] ? 'q' : 'h';
        $mod            = Mods::field($type.'enter,'.$type.'must_enter')->where('id', $data['mod_id'])->find();
        $enter          = $mod[$type.'enter'];
        $mustEnter      = $mod[$type.'must_enter'];
        $where          = [
            ['id', 'in', $mustEnter],
        ];
        $fields         = Fields::field('id,f')->where($where)->select();
        foreach ($fields as $field) {
            if (!isset($data[$field['f']]) || !trim($data[$field['f']])) {
                exception('请完善信息后提交');
            }
        }
        
        $where  = [
            ['id', 'in', $enter],
        ];
        $fields = Fields::field('id,f,fform')->where($where)->select();
        foreach ($fields as $field) {
            switch ($field['fform']) {
                case 'timestamp(Y-m-d)': case 'timestamp(Y-m-d H:i:s)':
                    $data[$field['f']] = strtotime($data[$field['f']]);
                    break;
                case 'checkbox':
                    $data[$field['f']] = isset($data[$field['f']]) ? implode(',', $data[$field['f']]) : '';
                    break;
                case 'image': 
                    $data[$field['f']] = (isset($data[$field['f']]) && $data[$field['f']]) ? $data[$field['f']] : 0;
                    break;
                case 'file':
                    $arr = [];
                    if (isset($data[$field['f']]['name'])) {
                        foreach ($data[$field['f']]['name'] as $key => $val) {
                            $arr[$key] = [
                                'value' => $data[$field['f']]['value'][$key],
                                'name'  => $val,
                            ];
                        }
                        $data[$field['f']] = serialize($arr);
                    } else {
                        $data[$field['f']] = '';
                    }
                    break;
                 case 'photos':
                    $arr = [];
                    if (isset($data[$field['f']]['desc'])) {
                        foreach ($data[$field['f']]['desc'] as $key => $val) {
                            $arr[$key] = [
                                'value' => $data[$field['f']]['value'][$key],
                                'desc'  => $val,
                            ];
                        }
                        $data[$field['f']] = serialize($arr);
                    } else {
                        $data[$field['f']] = '';
                    }
                    break;
            }
        }
        return $data;
    }
}
